from datetime import datetime
import requests
import pandas as pd

def get_date(cookie: str):
    jessonid = cookie.split("JSESSIONID=")[1]
    cookie = {"cookie": cookie}
    now_date = datetime.now().strftime("%Y-%m-%d")
    response = requests.post(f"http://10.84.4.74/yyqer/kfdtgl/clgl/pczb_json.jsp?rq={now_date}",cookies=cookie).json()["res"]
    res = "|井号|模型预测|机理预测|\n"
    res += "|---|---|---|\n"
    i = 0
    for well in response:
        #获取这口井当前产量
        now_prod = requests.post(f"http://10.84.4.74/yyqer/scbb/r_scqx_json.jsp?jh={well[0]}&srq={now_date}&erq={now_date}", cookies=cookie).json()["res"][-1][3]
        #请求大模型预测
        predict_prod = requests.post(f"http://10.85.2.60:32080/33/8111/prod/predict?jsessionid={jessonid}&well_name={well[0]}").json()["prediction_up"]
        predict_prod = float(predict_prod) / 10000 - float(now_prod)
        if predict_prod < 0 :
            predict_prod = 0

        mechanism_prod = float(well[3]) - float(well[2])
        if mechanism_prod < 0 :
            mechanism_prod = 0

        predict_prod = str(round(predict_prod, 2))
        mechanism_prod = str(round(mechanism_prod, 2))
        res += f"|{well[0]}|{predict_prod}|{mechanism_prod}|\n"
        i += 1
        print(f"{well[0]}完成|{well[0]}|{predict_prod}|{mechanism_prod}|:，已完成井数：{i}")
    return res

res = get_date("userName=wlx2023.jhyt; password=; JSESSIONID=E6F439D0E23B6B920D580D0E35190E7E")
print(res)
with open("./data.txt","w") as f:
    f.write(res)